﻿/*
 * Copyright (c) 2011 Shawn Rakowski
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to 
 * deal in the Software without restriction, including without limitation the 
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
 * sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 * SOFTWARE. 
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ROS.Application.DTO;

namespace ROS.Application
{
    /// <summary>
    /// Acts as a facade to the ROS Application.
    /// </summary>
    public class ROSApplication
    {
        #region Fields

        private UserService userService;
        private MenuService menuService;
        private OrderService orderService;

        #endregion

        #region Constructor

        public ROSApplication()
        {
            userService = new UserService();
            menuService = new MenuService();
            orderService = new OrderService();
        }

        #endregion

        #region User Services

        public ROSStatus AddUser(string firstName, string lastName, bool isManager, out long userID)
        {
            userID = 0;
            ROSStatus status = ROSStatus.INVALID;
            try
            {
                status = userService.AddUser(firstName, lastName, isManager, out userID);
            }
            catch
            {
                status = ROSStatus.ERROR_UNHANDLED_EXCEPTION;
            }
            return status;
        }

        public ROSStatus GetUser(long userID, out UserDTO userDTO)
        {
            userDTO = null;
            ROSStatus status = ROSStatus.INVALID;
            try
            {
                status = userService.GetUser(userID, out userDTO);
            }
            catch
            {
                status = ROSStatus.ERROR_UNHANDLED_EXCEPTION;
            }
            return status;
        }

        public ROSStatus GetActiveUsers(out IList<UserDTO> activeUserList)
        {
            activeUserList = null;
            ROSStatus status = ROSStatus.INVALID;
            try
            {
                status = userService.GetActiveUsers(out activeUserList);
            }
            catch
            {
                status = ROSStatus.ERROR_UNHANDLED_EXCEPTION;
            }
            return status;
        }

        public ROSStatus SetUserManagerialStatus(long userID, bool isManager)
        {
            ROSStatus status = ROSStatus.INVALID;
            try
            {
                status = userService.SetUserManagerialStatus(userID, isManager);
            }
            catch
            {
                status = ROSStatus.ERROR_UNHANDLED_EXCEPTION;
            }
            return status;
        }

        public ROSStatus DeleteUser(long userID)
        {
            ROSStatus status = ROSStatus.INVALID;
            try
            {
                status = userService.DeleteUser(userID);
            }
            catch
            {
                status = ROSStatus.ERROR_UNHANDLED_EXCEPTION;
            }
            return status;
        }

        #endregion

        #region Menu Services

        public long AddMenuCategory(String categoryName)
        {
            return menuService.AddMenuCategory(categoryName);
        }

        public ROSStatus GetMenuCategories(out IList<MenuCategoryDTO> menuCategories)
        {
            menuCategories = null;
            ROSStatus status = ROSStatus.INVALID;
            try
            {
                status = menuService.GetMenuCategories(out menuCategories);
            }
            catch
            {
                status = ROSStatus.ERROR_UNHANDLED_EXCEPTION;
            }
            return status;
        }

        public void DeleteMenuCategory(long menuCategoryID)
        {
            menuService.DeleteMenuCategoryByID(menuCategoryID);
        }

        public long AddMenuItem(String name, bool isBackendFulfilled,
                                   decimal price, IList<long> categoryIDs)
        {
            return menuService.AddMenuItem(name, isBackendFulfilled, price, categoryIDs);
        }

        public IList<MenuItemDTO> GetMenuItems()
        {
            return menuService.GetMenuItems();
        }

        public ROSStatus GetMenuItemsByCategory(long categoryID, out IList<MenuItemDTO> menuItems)
        {
            menuItems = null;
            ROSStatus status = ROSStatus.INVALID;
            //try
            //{
                status = menuService.GetMenuItemsByCategory(categoryID, out menuItems);
            //}
            //catch
            //{
            //    status = ROSStatus.ERROR_UNHANDLED_EXCEPTION;
            //}
            return status;
        }

        public void DeleteMenuItem(long menuItemID)
        {
            menuService.DeleteMenuItemByID(menuItemID);
        }

        #endregion

        #region Order Services

        public ROSStatus NewOrder(long userID, int tableNumber, int guestCount, string notes, out long orderNumber)
        {
            return orderService.NewOrder(userID, tableNumber, guestCount, notes, out orderNumber);
        }

        public IList<OrderDTO> GetActiveOrders()
        {
            return orderService.GetActiveOrders();
        }

        public OrderDTO GetOrder(long orderID)
        {
            return orderService.GetOrderByID(orderID);
        }

        public ROSStatus AddMenuItemToOrder(long orderID, long menuItemID, int quantity)
        {
            ROSStatus status = ROSStatus.INVALID;
            try
            {
                status = orderService.AddMenuItemToOrder(orderID, menuItemID, quantity);
            }
            catch
            {
                status = ROSStatus.ERROR_UNHANDLED_EXCEPTION;
            }
            return status;
        }

        #endregion
    }
}
